package com.itheima.thread;

/*
* 定义一个类SellTicket实现Runnable接口，里面定义一个成员变量：private int tickets = 100;
在SellTicket类中重写run()方法实现卖票，代码步骤如下
判断票数大于0，就卖票，并告知是哪个窗口卖的
卖了票之后，总票数要减1
票没有了，也可能有人来问，所以这里用死循环让卖票的动作一直执行
定义一个测试类SellTicketDemo，里面有main方法，代码步骤如下
创建SellTicket类的对象
创建三个Thread类的对象，把SellTicket对象作为构造方法的参数，并给出对应的窗口名称
启动线程
* */
public class Video implements Runnable {
    private int tickets = 100;
    int i=0;

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (tickets > 0) {
                System.out.println( Thread.currentThread().getName()+"出售第"+i+"张票");
                i--;
                tickets--;
            }
        }
    }
}

class Test {
    public static void main(String[] args) throws InterruptedException {
        Video v = new Video();
        Thread thread1 = new Thread(v, "一号窗口");
        Thread thread2 = new Thread(v, "二号窗口");
        Thread thread3 = new Thread(v, "三号窗口");
//        thread1.sleep(1000);
//        thread1.sleep(1000);
//        thread1.sleep(1000);1000
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
